<?php /** @noinspection SpellCheckingInspection */

class Util
{
    public static function ZipStatusString( $status ): string
    {
        switch( (int) $status )
        {
            case ZipArchive::ER_OK           : return 'N No error';
            case ZipArchive::ER_MULTIDISK    : return 'N Multi-disk zip archives not supported';
            case ZipArchive::ER_RENAME       : return 'S Renaming temporary file failed';
            case ZipArchive::ER_CLOSE        : return 'S Closing zip archive failed';
            case ZipArchive::ER_SEEK         : return 'S Seek error';
            case ZipArchive::ER_READ         : return 'S Read error';
            case ZipArchive::ER_WRITE        : return 'S Write error';
            case ZipArchive::ER_CRC          : return 'N CRC error';
            case ZipArchive::ER_ZIPCLOSED    : return 'N Containing zip archive was closed';
            case ZipArchive::ER_NOENT        : return 'N No such file';
            case ZipArchive::ER_EXISTS       : return 'N File already exists';
            case ZipArchive::ER_OPEN         : return 'S Can\'t open file';
            case ZipArchive::ER_TMPOPEN      : return 'S Failure to create temporary file';
            case ZipArchive::ER_ZLIB         : return 'Z Zlib error';
            case ZipArchive::ER_MEMORY       : return 'N Malloc failure';
            case ZipArchive::ER_CHANGED      : return 'N Entry has been changed';
            case ZipArchive::ER_COMPNOTSUPP  : return 'N Compression method not supported';
            case ZipArchive::ER_EOF          : return 'N Premature EOF';
            case ZipArchive::ER_INVAL        : return 'N Invalid argument';
            case ZipArchive::ER_NOZIP        : return 'N Not a zip archive';
            case ZipArchive::ER_INTERNAL     : return 'N Internal error';
            case ZipArchive::ER_INCONS       : return 'N Zip archive inconsistent';
            case ZipArchive::ER_REMOVE       : return 'S Can\'t remove file';
            case ZipArchive::ER_DELETED      : return 'N Entry has been deleted';
            default: return sprintf('Unknown status %s', $status );
        }
    }
    public static function getip() {
        static $ip = '';
        $ip = $_SERVER['REMOTE_ADDR'];
        if(isset($_SERVER['HTTP_CDN_SRC_IP'])) {
            $ip = $_SERVER['HTTP_CDN_SRC_IP'];
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] AS $xip) {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        }
        return $ip;
    }

    public static function arrayFind(string $word, array $arr, string $field,string $outField='')
    {
        if (empty($arr[0])) {
            return false;
        }
        if (!isset($arr[0][$field])) {
            return false;
        }
        foreach ($arr as $v) {
            if ($word == $v[$field]) {
                return !empty($outField) ? $v[$outField] : $v;
            }
        }
        return null;
    }

    /**
     * @throws Exception
     */
    public static function del($dir,array $skip=[]){
        $skip = array_filter($skip);
        $skipFiles = array_merge(skipFiles(), $skip);
        $fileList = self::getFiles($dir);
        foreach ($fileList as $file) {
            if (!in_array($file, $skipFiles)) {
                if (is_file($dir . '/' . $file)) {
                    unlink($dir . '/' . $file);
                } elseif (is_dir($dir . '/' . $file)) {
                    self::do_rmdir($dir . '/' . $file . '/');
                }
            }
        }
    }

    public static function do_rmdir($dirname, $self = false): bool
    {
        //检查文件或目录是否存在
        if (!file_exists($dirname)) {
            return false;
        }
        //是文件进行删除
        if (is_file($dirname) || is_link($dirname)) {
            return unlink($dirname);
        }
        //开始读取目录
        $dir = dir($dirname);
        if ($dir) {
            while (false !== $entry = $dir->read()) {
                if ($entry == '.' || $entry == '..') {
                    continue;
                }
                //进行文件删除
                self::do_rmdir($dirname . '/' . $entry);
            }
        }
        //关闭目录
        $dir->close();
        //是否删除本身文件夹
        $self && rmdir($dirname);
        //成功返回
        return true;
    }
    public static function getFiles($path,$prefix=false):array
    {
        $files = scandir($path);
        unset($files[0]);
        unset($files[1]);
        $files = array_values($files);
        if ($prefix !== false) {
            $path = rtrim($path, '/') . '/';
        }
        if ($prefix == false) {
            return $files;
        } elseif ($prefix == true) {
            foreach ($files as $k=>$file) {
                $files[$k] = $path . $file;
            }
        } else {
            foreach ($files as $k=>$file) {
                $files[$k] = $prefix . $file;
            }
        }
        return $files;
    }

}